package com.xiaoluo.common.shell;

import java.io.IOException;
import java.io.PipedOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Project : XL-CD
 * Package : com.xiaoluo.common.shell
 * Created by Willian.
 * Date  : 2016/3/22 20:44.
 */
public class Command {

    private static Map<String,Integer> CTL_CMD = new HashMap<String, Integer>();
    private static Map<String,String> CMD = new HashMap<String, String>();

    private static byte LF = 13;
    private static byte CR = 10;

    static {
        CTL_CMD.put("^A",	    1);//	0x01	SOH	Start of Heading (报头开始)
        CTL_CMD.put("^B",       2);//	0x02	STX	Start of Text (正文开始)
        CTL_CMD.put("^C",	    3);//	0x03	ETX	End of Text (正文结束)
        CTL_CMD.put("^D",	    4);//	0x04	EOT	End of Transmission (传输结束)
        CTL_CMD.put("^E",	    5);//	0x05	ENQ	Enquiry (查询)
        CTL_CMD.put("^F",	    6);//	0x06	ACK	Acknowledge (确认)
        CTL_CMD.put("^G",	    7);//	0x07	BEL	Bell (振铃)
        CTL_CMD.put("^H",	    8);//	0x08	BS	Backspace (退格)
        CTL_CMD.put("^I",	    9);//	0x09	HT	Horizontal Tab (水平制表)
        CTL_CMD.put("^J",	    10);//	0x0A	LF	Line Feed (馈行)
        CTL_CMD.put("^K",	    11);//	0x0B	VT	Vertical Tab (垂直制表)
        CTL_CMD.put("^L",	    12);//	0x0C	FF	Form Feed (馈页)
        CTL_CMD.put("^M",	    13);//	0x0D	CR	Carriage Return (回车)
        CTL_CMD.put("^N",	    14);//	0x0E	SO	Shift Out (移出)
        CTL_CMD.put("^O",	    15);//	0x0F	SI	Shift In (移入)
        CTL_CMD.put("^P",	    16);//	0x10	DLE	Data Link Escape (数据链路转义)
        CTL_CMD.put("^Q",	    17);//	0x11	DC1	Device Control 1 (设备控制1)
        CTL_CMD.put("^R",	    18);//	0x12	DC2	Device Control 2 (设备控制2)
        CTL_CMD.put("^S",	    19);//	0x13	DC3	Device Control 3 (设备控制3)
        CTL_CMD.put("^T",	    20);//	0x14	DC4	Device Control 4 (设备控制4)
        CTL_CMD.put("^U",	    21);//	0x15	NAK	Negative Acknowledge (否认)
        CTL_CMD.put("^V",	    22);//	0x16	SYN	Synchronous Idle (同步空闲)
        CTL_CMD.put("^W",	    23);//	0x17	ETB	End of Transmission Block (传输块结束)
        CTL_CMD.put("^X",	    24);//	0x18	CAN	Cancel (取消)
        CTL_CMD.put("^Y",	    25);//	0x19	EM	End of Medium (介质结束)
        CTL_CMD.put("^Z",	    26);//	0x1A	SUB	Substitute (替换)
        CTL_CMD.put("^[",	    27);//	0x1B	ESC	Escape (转义)
        CTL_CMD.put("^\\",	    28);//	0x1C	FS	File Separator (文件分隔符)
        CTL_CMD.put("^]",	    29);//	0x1D	GS	Group Separator (分组符)
        CTL_CMD.put("^^",	    30);//	0x1E	RS	Record Separator (记录分隔符)
        CTL_CMD.put("^_",	    31);//	0x1F	US	Unit Separator (单元分隔符)
        //=====================================================================
        CMD.put("quit","exit");
    }

    /**
     *
     * @param ip
     * @param user
     * @param pwd
     * @param outputStream
     * @throws Exception
     */
    public static void login(String ip,String user,String pwd, PipedOutputStream outputStream) throws Exception{
        String login = "ssh "+user+"@"+ip;
        outputStream.write(login.getBytes());
        outputStream.write(LF);
        outputStream.write(CR);
        outputStream.flush();
        /*
        outputStream.write(pwd.getBytes());
        outputStream.write(CR);
        outputStream.flush();
        outputStream.write("ls".getBytes());
        outputStream.write(CR);
        */

    }

    public static void answer(PipedOutputStream outputStream){
        String yes = "yes";
        try {
            outputStream.write(yes.getBytes());
            outputStream.write(CR);
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void pwd(PipedOutputStream outputStream){
        String root = "root";
        try {
            outputStream.write(root.getBytes());
            outputStream.write(CR);
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    /**
     *
     * @param cmd
     * @param outputStream
     * @throws Exception
     */
    public static void exec(String cmd,PipedOutputStream outputStream) throws Exception{

        if (CMD.get(cmd.toLowerCase()) != null){
            cmd = CMD.get(cmd.toLowerCase());
            outputStream.write(cmd.getBytes());
            outputStream.write(CR);
        }else if (CTL_CMD.get(cmd) != null){
            int cmdVal = CTL_CMD.get(cmd);
            outputStream.write(cmdVal);
        }else {
            outputStream.write(cmd.getBytes());
        }
        outputStream.flush();
    }
}
